#!/bin/bash

# 确保脚本在blink-ts目录下运行
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

# 从blink代码中提取已实现的系统调用
BLINK_DIR="../blink"

# 从syscall.c中提取SYSCALL宏定义的系统调用名称
grep -o 'SYSCALL[^,]*"[^\"]*"' "$BLINK_DIR/syscall.c" | 
  sed 's/^.*"//' | 
  sed 's/".*$//' | 
  sort | uniq > implemented_syscalls.txt

# 从当前syscall.txt文件中提取已实现的系统调用（如果存在）
if [ -f "syscall.txt" ]; then
  cat syscall.txt | 
    grep -v '^$' | 
    grep -v '^#' | 
    grep -v '以下是' | 
    sort | uniq >> implemented_syscalls_temp.txt
  sort implemented_syscalls_temp.txt | uniq > implemented_syscalls.txt
  rm implemented_syscalls_temp.txt
fi

# 创建一个常用Linux系统调用列表
cat > common_linux_syscalls.txt << EOF
access
accept
accept4
acct
add_key
alarm
arch_prctl
bind
bpf
brk
capget
capset
chdir
chmod
chown
chroot
clock_adjtime
clock_getres
clock_gettime
clock_nanosleep
clock_settime
clone
close
close_range
connect
copy_file_range
creat
delete_module
dup
dup2
dup3
epoll_create
epoll_create1
epoll_ctl
epoll_pwait
epoll_pwait2
epoll_wait
execve
exit
exit_group
faccessat
faccessat2
fallocate
fanotify_init
fanotify_mark
fchdir
fchmod
fchmodat
fchown
fchownat
fcntl
fdatasync
fgetxattr
flock
fork
fremovexattr
fsetxattr
fstat
fstatat
fstatfs
fsync
ftruncate
futex
futex_waitv
futex_wake
getcpu
getcwd
getdents
getdents64
getegid
geteuid
getgid
getgroups
gethostname
getitimer
getpeername
getpgid
getpgrp
getpid
getppid
getpriority
getrandom
getrlimit
get_robust_list
getsid
getsockname
getsockopt
gettid
gettimeofday
getuid
getxattr
give_page
hostname
ihash
init_module
io_cancel
ioctl
io_getevents
io_setup
io_submit
ioperm
iopl
ipc
kill
klogctl
lchown
lgetxattr
link
linkat
listen
listxattr
llseek
lseek
lsetxattr
lstat
madvise
memfd_create
migrate_pages
mincore
mkdir
mkdirat
mknod
mknodat
mlock
mlockall
mmap
mmap2
mprotect
mremap
msgctl
msgget
msgrcv
msgsnd
msync
munlock
munlockall
munmap
name_to_handle_at
nanosleep
newfstatat
open
openat
open_by_handle_at
pause
pciconfig_iobase
pciconfig_read
pciconfig_write
personality
pipe
pipe2
pivot_root
poll
ppoll
pread
pread64
preadv
preadv2
prctl
process_vm_readv
process_vm_writev
prlimit64
pselect6
pwrite
pwrite64
pwritev
pwritev2
quotactl
read
readahead
readlink
readlinkat
readv
reboot
recv
recvfrom
recvmmsg
recvmsg
remap_file_pages
removexattr
rename
renameat
renameat2
request_key
restart_syscall
rmdir
rseq
rt_sigaction
rt_sigpending
rt_sigprocmask
rt_sigqueueinfo
rt_sigreturn
rt_sigsuspend
rt_tgsigqueueinfo
sched_getaffinity
sched_getattr
sched_getparam
sched_get_priority_max
sched_get_priority_min
sched_getscheduler
sched_rr_get_interval
sched_setaffinity
sched_setattr
sched_setparam
sched_setscheduler
sched_yield
seccomp
select
semctl
semget
semop
semtimedop
send
sendfile
sendfile64
sendmmsg
sendmsg
sendto
set_robust_list
set_thread_area
set_tid_address
sethostname
setsid
setsockopt
setpgid
setpriority
setregid
setresgid
setresuid
setreuid
setrlimit
settimeofday
setuid
setxattr
shutdown
signalfd
signalfd4
sigaltstack
sigpending
sigprocmask
sigreturn
sigsuspend
socket
socketcall
socketpair
spu_create
spu_run
stat
statfs
statx
stime
symlink
symlinkat
sync
sync_file_range
syscalls
sysctl
sysinfo
syslog
sysret
tgkill
timer_create
timer_delete
timer_getoverrun
timer_gettime
timer_settime
times
tkill
truncate
ugetrlimit
umask
umount
umount2
uname
unlink
unlinkat
unshare
ustat
utime
utimensat
utimes
vfork
vmsplice
wait
wait4
waitid
waitpid
write
writev
EOF

# 排序两个文件
sort implemented_syscalls.txt -o implemented_syscalls.txt
sort common_linux_syscalls.txt -o common_linux_syscalls.txt

# 找出未实现的系统调用
comm -23 common_linux_syscalls.txt implemented_syscalls.txt > unimplemented_syscalls.txt

# 输出结果信息
IMPLEMENTED_COUNT=$(wc -l < implemented_syscalls.txt)
UNIMPLEMENTED_COUNT=$(wc -l < unimplemented_syscalls.txt)

cat << EOF
扫描完成！
已实现的系统调用数: $IMPLEMENTED_COUNT
未实现的系统调用数: $UNIMPLEMENTED_COUNT

未实现的系统调用列表已保存到: unimplemented_syscalls.txt
EOF

# 生成最终的结果文件，包含已实现和未实现的系统调用
cat > syscall_summary.txt << EOF
Blink系统调用实现状态总结

已实现的系统调用（$IMPLEMENTED_COUNT个）:
$(cat implemented_syscalls.txt)

未实现的系统调用（$UNIMPLEMENTED_COUNT个）:
$(cat unimplemented_syscalls.txt)
EOF

# 将未实现的系统调用添加到原syscall.txt文件
if [ -f "syscall.txt" ]; then
  echo "

以下是blink中尚未实现的常见系统调用（$UNIMPLEMENTED_COUNT个）：" >> syscall.txt
  cat unimplemented_syscalls.txt >> syscall.txt
  echo "

系统调用实现状态总结已保存到: syscall.txt"
fi

# 清理临时文件
sed -i '' '/^以下是blink中尚未实现的常见系统调用/,/^$/d' syscall.txt
echo "

以下是blink中尚未实现的常见系统调用（$UNIMPLEMENTED_COUNT个）：" >> syscall.txt
cat unimplemented_syscalls.txt >> syscall.txt

rm -f implemented_syscalls.txt common_linux_syscalls.txt unimplemented_syscalls.txt